package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.activity.common.DropImgsShowActivity;
import com.stn.zjx.givingtreasure.activity.dynamic.DynamicDetailActivity;
import com.stn.zjx.givingtreasure.bean.DynamicDetail;
import com.stn.zjx.givingtreasure.utils.ToolsUtils;
import com.vondear.rxtools.RxActivityTool;

import java.util.ArrayList;
import java.util.List;

public class DynamicRecommandListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<DynamicDetail> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public Context context;
   public DynamicRecommandListAdapter(Context context) {
       inflater = LayoutInflater.from(context);

      this.context=context;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_dynamic_recommand_detail, parent, false));
       return vh;
   }

   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
                   IndexHolder ih= (IndexHolder) viewHolder;
       DynamicDetail       dataBean=dataBeanList.get(i);
       String[] orign_imgs=dataBean.getDynamicImgs().split("\\*");
       List<String> new_imgs=new ArrayList<>();
       for(int o=0;o<orign_imgs.length;o++){
           if(!TextUtils.isEmpty(orign_imgs[o])){
               new_imgs.add(orign_imgs[o]);
           }
       }

        if(new_imgs.size() != 0){
            ih.iv.setVisibility(View.VISIBLE);
            Glide.with(context).load(new_imgs.get(0))
                    .transition(DrawableTransitionOptions.withCrossFade(500))
                    .apply(new RequestOptions()
                            .placeholder(new ColorDrawable(ActivityCompat.getColor(context,R.color.light_gray))))
                    .into(new SimpleTarget<Drawable>() {
                        @Override
                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                            ih.iv.setImageDrawable(resource);
                        }
                    });

        }else{
           ih.iv.setVisibility(View.GONE);
        }
         ih.content.setText(dataBean.getDynamicContent());
        ih.see_count.setText(String.valueOf(dataBean.getDynamicSeeCount()));
        ih.date_time.setText(ToolsUtils.getDateBeforeAfter(dataBean.getDynamicCreatedate()));
        ih.user_nickname.setText(dataBean.getUserNickname());

        ih.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                bundle.putInt("id",dataBean.getUserDynamicId());
                RxActivityTool.skipActivity(context, DynamicDetailActivity.class,bundle);
            }
        });

   }

   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<DynamicDetail> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void appendList(List<DynamicDetail> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }


   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
             public ImageView iv;
             public TextView content,date_time,user_nickname,see_count;
        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
            iv=itemView.findViewById(R.id.iv);
            content=itemView.findViewById(R.id.content);
            date_time=itemView.findViewById(R.id.date_time);
            user_nickname=itemView.findViewById(R.id.user_nickname);
            see_count=itemView.findViewById(R.id.see_count);
        }
   }



}